//
//  YahooCurrencyExchangeHtmlInterpreter.m
//  MarketDataColloctor
//
//  Created by jiro on 10/03/11.
//  Copyright 2010 jiro music. All rights reserved.
//

#import "YahooCurrencyExchangeHtmlInterpreter.h"
#import "../../../DbTypes/DbTypes.h"

@implementation YahooCurrencyExchangeHtmlInterpreter

- (id) interpret: (NSString*) html
{
	CurrencyExchangeJaList* list = [ [ CurrencyExchangeJaList alloc ] init ];

	// ---- 前提条件チェック ---- //
	if ( [ html isEqualToString: @"" ] ||
		 [ html rangeOfString: @"該当する期間のデータはありません。" ].location != NSNotFound ||
		 [ html rangeOfString: @"この検索期間の価格データはありません" ].location != NSNotFound ||
		 [ html rangeOfString: @"の価格データはありません" ].location != NSNotFound )
	{
		return list;
	}
	// ----------------------- //

//	NSLog( @"%@", html );

	NSError* error = nil;
	NSXMLDocument* document =
		[ [ NSXMLDocument alloc ] initWithXMLString: html options: NSXMLDocumentTidyHTML error: & error ];
	if ( error == nil )
	{
		errorType = HtmlInterpretError_ReadFile;
		return nil;
	}

	NSXMLElement* element = [ document rootElement ];
	NSXMLNode* node = element;

	//	通貨コードを取得
	error = nil;
	NSArray* codeNodes = [ node nodesForXPath: 
		@"/html/body/div[@id=\"wrapper\"]/div[@id=\"contents\"]/div[@id=\"contents-body\"]/div[@id=\"main\"]/div[ 1 ]/h1/strong"
		error: & error ];

	NSString* code;
	NSString* targetCurrencyCode;
	NSString* s = [ NSString stringWithString: [ [ codeNodes objectAtIndex: 0 ] stringValue ] ];
	s = [ s stringByReplacingOccurrencesOfString: @"【" withString: @"" ];
	code = [ s substringWithRange: NSMakeRange( 0, 3 ) ];
	targetCurrencyCode = [ s substringWithRange: NSMakeRange( 3, 3 ) ];
	error = nil;

	@try {

		//	取引データを取得
		NSArray* nodes = [ node nodesForXPath:
			@"/html/body/div[@id=\"wrapper\"]/div[@id=\"contents\"]/div[@id=\"contents-body\"]/div[@id=\"main\"]/div[ 6 ]/table/tr"
			error: & error ];

		NSEnumerator* e = [ nodes objectEnumerator ];
		NSXMLNode* exchangeNode;
		NSString* value;
		CurrencyExchangeJa* exchange;

		[ e nextObject ];	// skip title
		while ( exchangeNode = [ e nextObject ] )
		{
//			NSLog( @"%@", exchangeNode );

			exchange = [ [ CurrencyExchangeJa alloc ] init ];

			exchange.code = code;
			exchange.targetCurrencyCode = targetCurrencyCode;

			//	exchange date
			value = [ [ [ exchangeNode childAtIndex: 0 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"年" withString: @"-" ];
			value = [ value stringByReplacingOccurrencesOfString: @"月" withString: @"-" ];
			value = [ value stringByReplacingOccurrencesOfString: @"日" withString: @"" ];
			value = [ NSString stringWithFormat: @"%@ 00:00:00 +0900", value ];
			exchange.exchangeDate = [ NSDate dateWithNaturalLanguageString: value ];

			//	start price
			value = [ [ [ exchangeNode childAtIndex: 1 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"," withString: @"" ];
			exchange.openPrice = [ value floatValue ]; 

			//	heighest price
			value = [ [ [ exchangeNode childAtIndex: 2 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"," withString: @"" ];
			exchange.highPrice = [ value floatValue ]; 
			
			//	lowest price
			value = [ [ [ exchangeNode childAtIndex: 3 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"," withString: @"" ];
			exchange.lowPrice = [ value floatValue ]; 

			//	end price
			value = [ [ [ exchangeNode childAtIndex: 4 ] childAtIndex: 0 ] stringValue ];
			value = [ value stringByReplacingOccurrencesOfString: @"," withString: @"" ];
			exchange.closePrice = [ value floatValue ]; 
			
			[ list addObject: exchange ];
		}
		//	---
	}
	@catch ( NSException* exception )
	{
		errorType = HtmlInterpretError_UnknownHtmlNode;
		NSLog( @"HTMLの解析に失敗　コード: %@", code );
		return nil;
	}
	return list;
}

@end
